home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6770 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.1 KB

  1. Path: newshost.cyberramp.net!news
  2. From: sinan@cyberramp.net (John Noland)
  3. Newsgroups: comp.os.msdos.programmer,comp.lang.c
  4. Subject: Re: open vs fopen?
  5. Date: 15 Feb 1996 00:31:27 GMT
  6. Organization: Prose Software
  7. Message-ID: <4ftusv$181@newshost.cyberramp.net>
  8. References: <uEYFxc9nX8WX083yn@mbnet.mb.ca> <4f8bev$6tr@hermes.louisville.edu> <2d3avbl60.alamito@marketgraph.xs4all.nl>
  9. NNTP-Posting-Host: ramp3-11.cyberramp.net
  10. X-Newsreader: WinVN 0.99.5
  11.  
  12. natewild@mbnet.mb.ca (Nathan T. Wild) writes:
  13. >
  14. >In C, why would one use open and DOS int handles rather than fopen and
  15. >stdio file handles?
  16. >
  17. >The only reason I know of is UNIX portability.  Whenever I run across a
  18. >unix program that uses the old-style open instead of fopen I usually
  19. >end up rewriting it anyway though; I've had so many problems with
  20. >the unix-style stuff I just rather would deal with the ANSI style.
  21. >
  22. >David
  23. >>>Is there some speed advantage or differnet functionality?
  24. >
  25. >fopen() is mostly buffered, open() is, I believe, not. open() also gives some 
  26. >extra possible flags for sharing options. I use fopen() without problems. 
  27. >Besides, it's ANSI, and I program multiple programs and sometimes share 
  28. >modules that I'd have to check and redesign when switching from fopen to the 
  29. >local open command.
  30.  
  31. The open() function and its ilk are normally referred to as the "low-level"
  32. I/O package. fopen() is the "Buffered" or "Standard" I/O package. The 
  33. strength of the low-level I/O functions is that they offer excellent control,
  34. particularly when used with binary files. If you have a special I/O need, you
  35. can use the low-level I/O routines to fashion the exact I/O package to fit
  36. your needs. 
  37. The standard I/O package is one such creation. It is designed to provide fast
  38. buffered I/O, mostly for text situations. For a lot of applications, using the
  39. standard I/O package is simpler and more effective than using simple low-level
  40. output. The essential feature is its use of automatic buffering. Buffered 
  41. I/O means reading and writing data in large chunks from a file to an array
  42. and back. Reading and writing data in large chunks greatly speeds up the I/O
  43. operations, while storing in an array allows access to the individual bytes.
  44. The advantages of this approach should be apparent.
  45. When fopen() is used, several things happen. The file, of course, is opened.
  46. Second, an external character array is created to act as a buffer. The 
  47. <stdio.h> file has this buffer set to a size of 512 bytes. Third, a structure
  48. is created to contain information about the buffer and the file. When you 
  49. call one of the functions from the standard I/O library, such as getc(),
  50. the buffer is filled with data from the file. When getc() reaches the end of
  51. the buffer, the buffer is automatically with the next block of data from the
  52. file. You need not even be aware of the buffer, and you can program as if the
  53. I/O functions worked directly with the file.
  54. Technically, in DOS, all I/O is buffered since DOS itseld uses buffers
  55. for disk I/O. This is a transparent function of the operating system and
  56. has nothing to do with I/O packages included with C.
  57.  
  58. Hope this clarifies things.
  59.  
  60. -John Noland
  61.  
  62.